﻿
using System;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Media;
namespace YoucaiReader.Helper.Drawing
{
    public sealed class FontImpl : Font
    {
        private bool bold;
        private string familyName;
        private FontInfo fontInfo;
        private int fontSize;
        private bool italic;
        private string key;
        private StringMeasurements measurements;
        private int spaceCharWidth = -1;
        private Style style;
        private bool underline;

        public FontImpl(FontInfo fontInfo, string familyName, int size, bool bold, bool italic, bool underline)
        {
            this.fontInfo = fontInfo;
            this.familyName = familyName;
            this.fontSize = size;
            this.bold = bold;
            this.italic = italic;
            this.underline = underline;
            this.style = this.createStyle();
            this.measurements = this.fontInfo.GetMeasurements(this, " ");
            this.spaceCharWidth = (int)Math.Round((double)this.measurements.Width);
        }

        public int charsWidth(char[] ch, int offset, int length)
        {
            this.measurements = this.fontInfo.GetMeasurements(this, new string(ch, offset, length));
            return (int)Math.Round((double)this.measurements.Width);
        }

        private Style createStyle()
        {
            Style style2 = new Style();
            style2.TargetType = (typeof(TextBlock));
            Style style = style2;
            if (!string.IsNullOrEmpty(this.familyName))
            {
                style.Setters.Add(new Setter(TextBlock.FontFamilyProperty, new FontFamily(this.familyName)));
            }
            style.Setters.Add(new Setter(TextBlock.FontStyleProperty, this.italic ? FontStyles.Italic : FontStyles.Normal));
            style.Setters.Add(new Setter(TextBlock.FontWeightProperty, this.bold ? FontWeights.Bold : FontWeights.Normal));
            style.Setters.Add(new Setter(TextBlock.FontSizeProperty, (this.fontSize * 4.0) / 3.0));
            style.Setters.Add(new Setter(TextBlock.ForegroundProperty, new SolidColorBrush(Colors.Black)));
            if (this.underline)
            {
                style.Setters.Add(new Setter(TextBlock.TextDecorationsProperty, TextDecorations.Underline));
            }
            style.Seal();
            return style;
        }

        public Font derive(int size)
        {
            return new FontImpl(this.fontInfo, this.familyName, size, this.bold, this.italic, this.underline);
        }

        public Font derive(bool bold, bool italic, bool underlined)
        {
            return new FontImpl(this.fontInfo, this.familyName, this.fontSize, bold, italic, underlined);
        }

        public int[] getAvailableSizes()
        {
            return FontFactoryImpl.SIZES;
        }

        public int getBaselinePosition()
        {
            return (int)Math.Round((double)this.measurements.Height);
        }

        public int getDescent()
        {
            return (int)Math.Round((double)this.measurements.Baseline);
        }

        public string getFamilyName()
        {
            return this.familyName;
        }

        public int getHeight()
        {
            return (int)Math.Round((double)this.measurements.LineHeight);
        }

        internal string getKey()
        {
            if (this.key == null)
            {
                this.key = this.familyName + this.fontSize.ToString() + this.bold.ToString() + this.italic.ToString() + this.underline.ToString();
            }
            return this.key;
        }

        public int getSize()
        {
            return this.fontSize;
        }

        internal Style getStyle()
        {
            return this.style;
        }

        public int getWhitespaceWidth()
        {
            return this.spaceCharWidth;
        }

        public bool isBold()
        {
            return this.bold;
        }

        public bool isItalic()
        {
            return this.italic;
        }

        public bool isUnderlined()
        {
            return this.underline;
        }

        public override string ToString()
        {
            return string.Concat(new object[] { "font name=", this.familyName, " size=", this.fontSize, " bold=", this.bold, " italic=", this.italic, " underline=", this.underline });
        }
    }
}

